package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JOptionPane;

public class Listener extends Thread
{
	private Socket socket = null;
	private boolean works;
	public PrintWriter out = null;
	private BufferedReader in = null;
	private String line = "";
	private PlayerFrame frame;
	private String name;
	private String shortName;
    private int[] temporaryHand;
	
	public Listener(int port, String IP, String name) {
		this.name = name;
		this.shortName = name.trim();
        this.temporaryHand = new int[4];
		System.out.println(">" + shortName + "<");
		try {
			this.socket  = new Socket(IP,port);	
		}
		catch (IOException e) {
			System.out.println("Socket problem");
			JOptionPane.showMessageDialog(null, "You cannot join the game\non this port" ,
					"Error", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
		
		this.works = true;
	}
	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			out = new PrintWriter(socket.getOutputStream(),true);
			out.println("Name_"+name);
		}
		catch(IOException e){
			System.out.println("or out failed");
		}
		while (line != null && works) {
			try {
				line = in.readLine();
				System.out.println("Line for " + name + ":" + line + "<");

				if (line.startsWith("addNewPlayer")){
					frame.setPlayer(line.substring(12), false);
				}
				else if (line.startsWith("Accepted_")) {
					frame = new PlayerFrame(name, this);
					out.println("Name_" + name);
					frame.setVisible(true);
				}
				else if (line.startsWith("BadName_")) {
					JOptionPane.showMessageDialog(null, "Name \"" + shortName + "\" is already used" ,
							"Error", JOptionPane.ERROR_MESSAGE);
					System.exit(1);
				}
				else if (line.startsWith("previousPlayers_")) {
					line = line.substring(16);
					String temp;
					while (!line.equals("")) {
						temp = line.substring(0,15);
						line = line.substring(15);
						frame.setPlayer(temp, true);
					}
				}
				else if (line.startsWith("Chat_")) {
					frame.displayMessage(line.substring(5));
				}
				else if (line.startsWith("SmallBlind_")) {
					frame.setSmallBlind(line.substring(11,26),line.substring(26));
				}
				else if (line.startsWith("BigBlind_")) {
					frame.setBigBlind(line.substring(9,24),line.substring(24));
				}
				else if (line.startsWith("Cards_")) {
                    for (int i = 0; i < 4; i++) {
                        temporaryHand[i] = Integer.parseInt(line.substring(6+2*i, 8+2*i));
                    }
                    frame.displayCards(temporaryHand);
				}
				else if (line.startsWith("sCoins_")) {
					frame.setCoins(line.substring(7,22),line.substring(22));
				}
				else if ( line.startsWith("Bet_")) {
					frame.setBet(line.substring(4,19),line.substring(19));
				}
				else if (line.startsWith("Enable_")) {
					frame.enableButtons(line.substring(7));
				}
				else if (line.startsWith("dFold_")) {
					frame.displayFold(line.substring(6));
				}
				else if (line.startsWith("dAllin_")) {
					frame.displayAllin(line.substring(7));
				}
				else if (line.startsWith("Stake_")) {
					frame.displayStake(line.substring(6));
				}
				else if (line.startsWith("Round_")) {
					frame.displayRound(line.substring(6));
				}
				else if (line.startsWith("Delete_")) {
					frame.deletePlayer(line.substring(7));
				}
				else if (line.startsWith("Amount_")) {
					frame.displayAmountOfExchangedCards(line.substring(7, 22),line.substring(22));
				}
				else if (line.startsWith("Winner_")) {
					String temp = line.substring(7);
	                if ((temp.length() % 24) != 0)
	                	System.out.println("BŁAD!!!!!!!!!!!!!");
	                int numberOfPlayers = temp.length() / 24;
	                String[] playersHand = new String[numberOfPlayers];
	                for (int i = 0; i < numberOfPlayers; i++) {
	                	playersHand[i] = temp.substring(23*i, 23*(i+1));
	                }
	                frame.displayPlayerHands(playersHand,temp.substring(23*numberOfPlayers));
				}
				else if (line.startsWith("Info_")) {
					frame.displayInfo(line.substring(5));
				}
			}
			catch (IOException e) {
				System.out.println("Read failed");
				JOptionPane.showMessageDialog(null, "Game was interrupted" ,
						"Error", JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
		}
	}
	public void sendMessage(String message) {
		out.println("Send_" + shortName + ":" + message);
	}
	public void sendCall() {
		out.println("Call_");
	}
	public void sendRaise(int bet) {
		out.println("Raise_" + Integer.toString(bet));
	}
	public void sendBet(int bet) {
		out.println("Bet_" + Integer.toString(bet));
	}
	public void sendCheck() {
		out.println("Check_");
	}
	public void sendAllin() {
		out.println("All_");
	}
	public void sendFold() {
		out.println("Fold_");
	}
	public void sendExchange(String cardSet) {
		out.println("Exchange_" + cardSet);
	}
	public void sendClose() {
		out.println("Close_");
	}
	public void sendEnd() {
		out.println("End_");
	}
	
}
